package co.za.itweb.brainstormlite;



import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;

public class Cio extends Activity{
	private int width;
	private WebView banner;
	private ProgressDialog progDialog;
	private String density;
	private int bannerHolderWidth;
	private int height;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cio);
		
		getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

		
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);

		 width = (int) metrics.widthPixels;
		 height = (int) metrics.heightPixels;
		 if(determineDensity()=="ldpi"){
				bannerHolderWidth=(int) (width*1.3);
			}else if(determineDensity()=="mdpi"){
				bannerHolderWidth=(int) (width);
			}else if(determineDensity()=="hdpi"){
				bannerHolderWidth=(int) (width)-(width/3);
			}else if(determineDensity()=="xdpi"){
				bannerHolderWidth=(int) (width)-(width/2);
			}
		 RelativeLayout linLayout = (RelativeLayout) findViewById(R.id.cio);
		 RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
		  
			 banner = new WebView(Cio.this); 
			 linLayout.addView(banner, layoutParams);
			 
			 progDialog = new ProgressDialog(this);
		        progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		        progDialog.setMessage("Loading ...");
		        progDialog.setCancelable(true);
		        progDialog.setMax(100);
		        progDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
			          public void onCancel(DialogInterface dialog) {
			             
			        	 
			    			finish();
			          }
			    });
		        progDialog.show();
			 banner.getSettings().setJavaScriptEnabled(true);
			 banner.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
             banner.setBackgroundColor(0);
			 banner.setWebViewClient(new myWebClient()); 
			 
			 banner.loadUrl("http://itweb.mobi/brainstorm/cio/index.php?start=0");
	}
	
	
	public class myWebClient extends WebViewClient  
    {  
		
        @Override  
        public void onPageStarted(WebView view, String url, Bitmap favicon) {  
        
            super.onPageStarted(view, url, favicon);  
        }  
          
        @Override  
        public boolean shouldOverrideUrlLoading(WebView view, String url) {  
           
              
        	getApplicationContext().startActivity(
		                new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
		            return true;
        
              
        }  
          
        @Override  
        public void onPageFinished(WebView view, String url) {  
           
            super.onPageFinished(view, url);  
            progDialog.dismiss();  
            
           
        }  
    }  
	
	public String determineDensity(){
		switch (getResources().getDisplayMetrics().densityDpi) {
		case DisplayMetrics.DENSITY_LOW:
			density="ldpi";
			
		    break;
		case DisplayMetrics.DENSITY_MEDIUM:
			density= "mdpi";
			
			 break;
		case DisplayMetrics.DENSITY_HIGH:
			density= "hdpi";
			
			 break;
		case DisplayMetrics.DENSITY_XHIGH:
			density= "xdpi";
			
			 break;
		}
		return density;
	}
}
